package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlSsxsjlbBsDetailBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlSsxsjlbBsDetailVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlSsxsjlbBsDetail;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlSsxsjlbBsDetailMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlSsxsjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料渗水系数试验检测记录详情Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlSsxsjlbBsDetailServiceImpl implements IReportLqhhlSsxsjlbBsDetailService {

    private final ReportLqhhlSsxsjlbBsDetailMapper baseMapper;

    /**
     * 查询沥青混合料渗水系数试验检测记录详情
     */
    @Override
    public ReportLqhhlSsxsjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青混合料渗水系数试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportLqhhlSsxsjlbBsDetailVo> queryPageList(ReportLqhhlSsxsjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlSsxsjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料渗水系数试验检测记录详情列表
     */
    @Override
    public List<ReportLqhhlSsxsjlbBsDetailVo> queryList(ReportLqhhlSsxsjlbBsDetailBo bo) {
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlSsxsjlbBsDetail> buildQueryWrapper(ReportLqhhlSsxsjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqhhlSsxsjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), ReportLqhhlSsxsjlbBsDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getChusjssdsl() != null, ReportLqhhlSsxsjlbBsDetail::getChusjssdsl, bo.getChusjssdsl());
        lqw.eq(bo.getShensl1() != null, ReportLqhhlSsxsjlbBsDetail::getShensl1, bo.getShensl1());
        lqw.eq(bo.getShensl2() != null, ReportLqhhlSsxsjlbBsDetail::getShensl2, bo.getShensl2());
        lqw.eq(bo.getShensl3() != null, ReportLqhhlSsxsjlbBsDetail::getShensl3, bo.getShensl3());
        lqw.eq(StringUtils.isNotBlank(bo.getShens500()), ReportLqhhlSsxsjlbBsDetail::getShens500, bo.getShens500());
        lqw.eq(bo.getShensxscz() != null, ReportLqhhlSsxsjlbBsDetail::getShensxscz, bo.getShensxscz());
        return lqw;
    }

    /**
     * 新增沥青混合料渗水系数试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportLqhhlSsxsjlbBsDetailBo bo) {
        ReportLqhhlSsxsjlbBsDetail add = BeanUtil.toBean(bo, ReportLqhhlSsxsjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青混合料渗水系数试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportLqhhlSsxsjlbBsDetailBo bo) {
        ReportLqhhlSsxsjlbBsDetail update = BeanUtil.toBean(bo, ReportLqhhlSsxsjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlSsxsjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料渗水系数试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
